package com.yyds.blackmagic.controller;

import com.yyds.blackmagic.Annotation.JwtAnno;
import com.yyds.blackmagic.entity.User;
import com.yyds.blackmagic.service.UserService;
import com.yyds.blackmagic.utils.JwtUtils;
import com.yyds.blackmagic.vo.ActionResult;
import com.yyds.blackmagic.vo.LoginInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author ccc
 * @Date 2021/12/13
 * @Descripton
 */

@RestController
@RequestMapping("/api")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/admin/user")
    public ActionResult findAll () {
        List<User> users = userService.findAll ();
        ActionResult ok = new ActionResult (200, "ok", users);
        return ok;

    }

    @PostMapping("/login")
    public ActionResult login (@RequestBody LoginInfoVo infoVo) {
        User user = userService.findByUsername (infoVo.getUsername ());
        if (user == null) {
            return new ActionResult (401, "user not exists", null);
        }
        if (infoVo.getPassword ().equals (user.getPassword ())) {
            //创建令牌
            String token = JwtUtils.createToken (infoVo.getUsername ());
            //返回响应的对象消息
            return new ActionResult (200, "login success", token);
        }
        return new ActionResult (401, "password error", null);
    }

    @PostMapping("/register")
    public ActionResult regist (@RequestBody User user) {
        User byName = userService.findByUsername (user.getUsername ());
        if (byName != null) {
            return new ActionResult (401, "user already exists", null);
        } else {
            User user1 = userService.addUser (user);
            if (user1 != null) {
                return new ActionResult (200, "regist success", user1);
            }
            return new ActionResult (401, "regist fail", null);
        }
    }


    @PostMapping("/user2")
    public ActionResult add (@RequestBody User user) {
        User user1 = userService.updateUser (user);
        ActionResult ok = new ActionResult (200, "ok", user1);
        return ok;
    }


    @PutMapping("/admin/user")
    public ActionResult update (@RequestBody User user) {
        System.out.println(user);
        User user1 = userService.updateUser (user);
        System.out.println(user1);
        ActionResult ok = new ActionResult (200, "ok", user1);
        return ok;
    }

    @PutMapping("/admin/user/password")
    public ActionResult setPassword (String username) {
        log.info(username);
        User user = userService.findByUsername(username);
        user.setPassword("123");
        User user1 = userService.updateUser(user);
        ActionResult ok = new ActionResult (200, "ok", user1);
        return ok;
    }


    @DeleteMapping("/admin/user{id}")
    public Boolean delete (@PathVariable int id) {
        boolean b = userService.deleteUser (id);
        return b;
    }

    @GetMapping("/logout")
    public ActionResult logOut () {
        return new ActionResult (200, "logout", null);
    }


    @GetMapping("/authentication")
    public String authentication () {
        return "身份认证成功";
    }
}
